import 'package:epidemic_data/data/data.dart';
import 'package:epidemic_data/http/http_util.dart';
import 'package:epidemic_data/model/gcov_response.dart';
import 'package:epidemic_data/model/ncov_response.dart';
import 'package:flutter/material.dart';

GlobalKey<_CountryStatsGridState> childKey = GlobalKey();
class CountryStatsGrid extends StatefulWidget {
  int index = 0;
  CountryStatsGrid(this.index,{required Key key,}) : super(key: key);

  @override
  _CountryStatsGridState createState() {
    return _CountryStatsGridState();
  }
}

class _CountryStatsGridState extends State<CountryStatsGrid> {

  int confirmedCount = 0;
  int totalComfirmedCount = 0;
  int curedCount = 0;
  int deadCount = 0;
  String deadRate = "";
  List list = [];



  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery
          .of(context)
          .size
          .height * 0.25,
      child: Column(
        children: <Widget>[
          Flexible(
              child: Row(
                children: <Widget>[
                  _buildStatsCard('当前确诊人数', confirmedCount.toString(), Colors.orange),
                  _buildStatsCard('总死亡数', deadCount.toString(), Colors.red),
                ],
              )),
          Flexible(
              child: Row(
                children: <Widget>[
                  _buildStatsCard('总康复数', curedCount.toString(), Colors.green),
                  _buildStatsCard('总确诊数', totalComfirmedCount.toString(), Colors.lightBlue),
                  _buildStatsCard('死亡率', deadRate+"%", Colors.purple),
                ],
              )),
        ],
      ),
    );
  }

  Expanded  _buildStatsCard(String title, String count, MaterialColor color){
    return Expanded(
      child: Container(
        margin: const EdgeInsets.all(8.0),
        padding: const EdgeInsets.all(10.0),
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              title,
              style: const TextStyle(
                color: Colors.white,
                fontSize: 15.0,
                fontWeight: FontWeight.w600,
              ),
            ),
            Text(
              count.toString(),
              style: const TextStyle(
                color: Colors.white,
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
    );
  }

  //获取网络数据
  void getData() {
    // HttpUtil.get("http://api.tianapi.com/ncovabroad/index", (data) {
    //   print("callback");
    //   GcovResponse response = GcovResponse.fromJson(data);
    //   setState(() {
    //     confirmedCount = response.newslist[widget.index].currentConfirmedCount;
    //     totalComfirmedCount = response.newslist[widget.index].confirmedCount;
    //     curedCount = response.newslist[widget.index].curedCount;
    //     deadCount = response.newslist[widget.index].deadCount;
    //     deadRate = response.newslist[widget.index].deadRate;
    //     list = response.newslist;
    //   });
    // },params: {"key":"132133af3d8bdfb0c50d265159e15983"}, errorCallBack: (errorMsg) {
    //   print("error:" + errorMsg);
    // });

    GcovResponse response = GcovResponse.fromJson(globalInfoFake);
    setState(() {
      confirmedCount = response.newslist[widget.index].currentConfirmedCount;
      totalComfirmedCount = response.newslist[widget.index].confirmedCount;
      curedCount = response.newslist[widget.index].curedCount;
      deadCount = response.newslist[widget.index].deadCount;
      deadRate = response.newslist[widget.index].deadRate;
      list = response.newslist;
    });
  }
}